#include "VNewProjectDialog.h"
#include "ui_VNewProjectDialog.h"

#include <QString>
#include <QFileDialog>
#include <QDir>
#include <QMessageBox>
#include <QDebug>
#include <QListView>

#include "VGlobal.h"
#include "VFunctions.h"

VNewProjectDialog::VNewProjectDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VNewProjectDialog)
{
    ui->setupUi(this);

    // 设置combobox 为了使得item高度可以改变
    ui->combobox_jz->setView(new QListView());
    ui->combobox_type->setView(new QListView());
    ui->combobox_brand->setView(new QListView());

    // 读取 config 的 project
    ui->combobox_brand->clear();
    ui->combobox_type->clear();

    QStringList list = VFunctions::get_folder_list(VGlobal::config_dir + "/project");
    foreach(QString path, list)
    {
        ui->combobox_brand->addItem(QFileInfo(path).fileName());
        ui->combobox_brand->setItemData(ui->combobox_brand->count()-1,
                                        QVariant::fromValue(VFunctions::get_folder_list(path)));
//        QStringList child_list = VFunctions::get_folder_list(path);
//        foreach(QString child_path, child_list)
//        {
//            ui->combobox_type->addItem(QFileInfo(child_path).fileName());
//        }
    }

    if(ui->combobox_brand->count() > 0)
    {
        on_combobox_brand_currentIndexChanged(0);
    }


}

VNewProjectDialog::~VNewProjectDialog()
{
    delete ui;
}

void VNewProjectDialog::on_btn_select_dir_clicked()
{
    QString path = QFileDialog::getExistingDirectory();
    ui->lineedit_dir->setText(path);
}

// 确认
void VNewProjectDialog::on_pushButton_clicked()
{
    _brand = ui->combobox_brand->currentText();
    _type = ui->combobox_type->currentText();
    _jz = ui->combobox_jz->currentText();
    _name = ui->lineedit_project_name->text();
    _dir = ui->lineedit_dir->text();

    // 判断数据合法性
    if(_type.length() == 0)
    {
        QMessageBox::critical(nullptr, "单片机类型错误", QString("错误 : 单片机类型不能为空"));
        return;
    }

    if(_jz.length() == 0)
    {
        QMessageBox::critical(nullptr, "单片机晶振错误", QString("错误 : 单片机晶振不能为空"));
        return;
    }

    if(_name.length() == 0)
    {
        QMessageBox::critical(nullptr, "工程名称错误", QString("错误 : 工程名称不能为空"));
        return;
    }

    if(_dir.length() == 0)
    {
        QMessageBox::critical(nullptr, "工程路径错误", QString("错误 : 工程路径不能为空"));
        return;
    }

    if(QDir(_dir).exists() == false)
    {
        QMessageBox::critical(nullptr, "路径不存在", QString("路径 : %1 \r\n错误 : 路径不存在").arg(_dir));
        return;
    }

    // 判断工程是否已经存在了
    if(QDir(_dir + "/" + _name).exists())
    {
        QMessageBox::critical(nullptr, "工程已经存在", QString("工程 : %1\r\n错误 : 此工程已经存在").arg(_name));
        return;
    }

    this->accept();
}

// 取消
void VNewProjectDialog::on_pushButton_2_clicked()
{
    this->reject();
}


void VNewProjectDialog::on_combobox_brand_currentIndexChanged(int index)
{
    QStringList list = ui->combobox_brand->itemData(index).value<QStringList>();

    ui->combobox_type->clear();
    foreach(QString path, list)
    {
        ui->combobox_type->addItem(QFileInfo(path).fileName());
    }
}
